package com.koron.order.risk.riskFollow.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.risk.riskFollow.bean.dto.LineRiskFollowDTO;
import com.koron.order.risk.riskFollow.bean.query.LineRiskFollowQuery;
import com.koron.order.risk.riskFollow.bean.vo.LineRiskFollowVO;
import com.koron.order.risk.riskFollow.service.LineRiskFollowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 风险跟进单(LineRiskFollow)表控制层
 *
 * @author jzp
 * @since 2022-12-16 11:33:16
 */
@Api(tags = "风险跟进单接口")
@RestController
@RequestMapping("lineRiskFollow")
public class LineRiskFollowController implements WorkflowController {
    @Autowired
    private LineRiskFollowService lineRiskFollowService;
    @Autowired
    private WorkFlowHandler workFlowHandler;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("detail")
    public Response<LineRiskFollowDTO> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), lineRiskFollowService, "getDtoById", LineRiskFollowDTO.class, id));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Response<PageInfo<LineRiskFollowVO>> page(PageQuery pageQuery, LineRiskFollowQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), lineRiskFollowService, "page", PageInfo.class, pageQuery, query));
    }

    @ApiOperation(value = "保存")
    @PostMapping("save")
    public Response<LineRiskFollowDTO> save(@RequestBody LineRiskFollowDTO lineRiskFollowDTO) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), lineRiskFollowService, "saveDto", LineRiskFollowDTO.class, lineRiskFollowDTO));
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("delete")
    public Response delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), lineRiskFollowService, "deleteById", Boolean.class, id);
        workFlowHandler.delete(id);
        return Response.ok();
    }

    @Override
    @ApiOperation(value = "end")
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        LineRiskFollowDTO lineRiskFollowDTO = JSON.parseObject(redirectQuery.getFormVariables(), LineRiskFollowDTO.class);
        // 提交
        if (redirectQuery.getOperation() == 32) {
            // 如果是结束
            if (redirectQuery.getCurNodeCode().equals("END")) {
                ADOConnection.runTask(getCurrDs(), lineRiskFollowService, "end", Boolean.class, lineRiskFollowDTO);
            } else {
                // 否则为提交
                lineRiskFollowDTO.setStatus("1");
                ADOConnection.runTask(getCurrDs(), lineRiskFollowService, "saveDto", LineRiskFollowDTO.class, lineRiskFollowDTO);
            }
        } else if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            // 如果是驳回或撤回，回到草稿状态
            ADOConnection.runTask(getCurrDs(), lineRiskFollowService, "updateStatus", Boolean.class, redirectQuery.getDataId(), "0");
        } else if (redirectQuery.getOperation() == 16) {
            // 如果是废弃，状态改为草稿，并删除流程
            ADOConnection.runTask(getCurrDs(), lineRiskFollowService, "updateStatus", Boolean.class, redirectQuery.getDataId(), "0");
            workFlowHandler.delete(redirectQuery.getDataId());
        }
        return Response.success();
    }

}

